package com.hhl.flower.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhl.flower.common.constant.CouponConstant;
import com.hhl.flower.common.constant.OrderConstant;
import com.hhl.flower.common.utils.OrderNumberUtil;
import com.hhl.flower.context.BaseContext;
import com.hhl.flower.mapper.*;
import com.hhl.flower.pojo.entity.*;
import com.hhl.flower.pojo.vo.OrderDetailVO;
import com.hhl.flower.pojo.vo.OrderSubmitVO;
import com.hhl.flower.pojo.vo.OrderVO;
import com.hhl.flower.service.OrdersService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author Liubuzhu
 * @description 针对表【orders】的数据库操作Service实现
 * @createDate 2024-06-12 14:38:47
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders>
		implements OrdersService{

	@Autowired
	private OrdersMapper ordersMapper;
	@Autowired
	private OrderDetailMapper orderDetailMapper;
	@Autowired
	private ShoppingCartMapper shoppingCartMapper;
	@Autowired
	private CouponMapper couponMapper;
	@Autowired
	private AddressMapper addressMapper;
	@Autowired
	private FlowerMapper flowerMapper;

	public List<OrderVO> listByStatus(Integer status) {
		Integer currentId = BaseContext.getCurrentId();
		QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("u_id", currentId);
		List<OrderVO> orderVOList = new ArrayList<>();
		if (status == 0) {
			// 默认查询所有
			List<Orders> orders = ordersMapper.selectList(queryWrapper);
			for (Orders order : orders) {
				OrderVO orderVO = new OrderVO();
				BeanUtils.copyProperties(order, orderVO);
				Address address = addressMapper.selectById(order.getAId());
				Coupon coupon = couponMapper.selectById(order.getCId());
				if (coupon != null) {
					orderVO.setCouponAmount(coupon.getDiscount());
				}
				orderVO.setConsignee(address.getConsignee());
				orderVO.setPhone(address.getPhone());
				orderVO.setDetail(address.getDetail());
				QueryWrapper<OrderDetail> queryWrapper1 = new QueryWrapper<>();
				queryWrapper1.eq("o_id", order.getId());
				List<OrderDetail> orderDetails = orderDetailMapper.selectList(queryWrapper1);
				List<OrderDetailVO> orderDetailVOList = new ArrayList<>();
				for (OrderDetail orderDetail : orderDetails) {
					OrderDetailVO orderDetailVO = new OrderDetailVO();
					BeanUtils.copyProperties(orderDetail, orderDetailVO);
					Flower flower = flowerMapper.selectById(orderDetail.getFId());
					if (flower != null) {
						BeanUtils.copyProperties(flower, orderDetailVO);
					}
					orderDetailVOList.add(orderDetailVO);
				}
				orderVO.setOrderDetailList(orderDetailVOList);
				orderVOList.add(orderVO);
			}
			return orderVOList;
		} else  {
			queryWrapper.eq("status", status);
			List<Orders> orders = ordersMapper.selectList(queryWrapper);
			for (Orders order : orders) {
				OrderVO orderVO = new OrderVO();
				BeanUtils.copyProperties(order, orderVO);
				Address address = addressMapper.selectById(order.getAId());
				Coupon coupon = couponMapper.selectById(order.getCId());
				if (coupon != null) {
					orderVO.setCouponAmount(coupon.getDiscount());
				}
				orderVO.setConsignee(address.getConsignee());
				orderVO.setPhone(address.getPhone());
				orderVO.setDetail(address.getDetail());
				QueryWrapper<OrderDetail> queryWrapper1 = new QueryWrapper<>();
				queryWrapper1.eq("o_id", order.getId());
				List<OrderDetail> orderDetails = orderDetailMapper.selectList(queryWrapper1);
				List<OrderDetailVO> orderDetailVOList = new ArrayList<>();
				for (OrderDetail orderDetail : orderDetails) {
					OrderDetailVO orderDetailVO = new OrderDetailVO();
					BeanUtils.copyProperties(orderDetail, orderDetailVO);
					Flower flower = flowerMapper.selectById(orderDetail.getFId());
					if (flower != null) {
						BeanUtils.copyProperties(flower, orderDetailVO);
					}
					orderDetailVOList.add(orderDetailVO);
				}
				orderVO.setOrderDetailList(orderDetailVOList);
				orderVOList.add(orderVO);
			}
			return orderVOList;
		}
	}

	public OrderSubmitVO submitOrder(List<Integer> sids, Integer aid, Integer cid, Double amount, String remark) {
		Integer currentId = BaseContext.getCurrentId();
		//向订单表插入一条数据
		Orders orders = new Orders();
		orders.setAId(aid);
		orders.setCId(cid);
		orders.setUId(currentId);
		orders.setAmount(amount);
		orders.setRemark(remark);
		orders.setOrderTime(LocalDateTime.now());
		orders.setStatus(OrderConstant.UNPAID);
		orders.setNumber(OrderNumberUtil.generateOrderNumber());

		ordersMapper.insert(orders);

		// 查找出对应购物车项信息
		for (Integer sid : sids) {
			QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("id", sid);
			ShoppingCart shoppingCart = shoppingCartMapper.selectOne(queryWrapper);
			OrderDetail orderDetail = new OrderDetail();
			orderDetail.setFId(shoppingCart.getFId());
			orderDetail.setNumber(shoppingCart.getNumber());
			orderDetail.setAmount(shoppingCart.getAmount());
			orderDetail.setOId(orders.getId());
			orderDetailMapper.insert(orderDetail);

			// 最后删除购物车项
//			shoppingCartMapper.deleteById(shoppingCart.getId());
		}

		OrderSubmitVO orderSubmitVO = new OrderSubmitVO();
		orderSubmitVO.setId(orders.getId());
		orderSubmitVO.setNumber(orders.getNumber());

		return orderSubmitVO;
	}

	public void payOrder(String number, Integer payMethod) {
		QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("number", number);
		Orders orders = ordersMapper.selectOne(queryWrapper);
		orders.setPayMethod(payMethod);
		orders.setStatus(OrderConstant.WAIT_DELIVERY);

		// 如果选择了优惠券，改变优惠券状态为已使用
		if (orders.getCId() != null) {
			QueryWrapper<Coupon> queryWrapper1 = new QueryWrapper<>();
			queryWrapper1.eq("id", orders.getCId());
			Coupon coupon = couponMapper.selectOne(queryWrapper1);
			if (coupon != null) {
				coupon.setStatus(CouponConstant.USED);
				couponMapper.updateById(coupon);
			}
		}

		// 花卉销量增加
		QueryWrapper<OrderDetail> queryWrapper1 = new QueryWrapper<>();
		queryWrapper1.eq("o_id", orders.getId());
		List<OrderDetail> orderDetails = orderDetailMapper.selectList(queryWrapper1);
		for (OrderDetail orderDetail : orderDetails) {
			Flower flower = flowerMapper.selectById(orderDetail.getFId());
			flower.setSales(flower.getSales() + orderDetail.getNumber());
			flower.setInventory(flower.getInventory() - orderDetail.getNumber());
			flowerMapper.updateById(flower);
		}

		ordersMapper.updateById(orders);
	}

	public void cancelOrder(String number) {
		// 取消订单
		QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("number", number);
		Orders orders = ordersMapper.selectOne(queryWrapper);
		if (Objects.equals(orders.getStatus(), OrderConstant.UNPAID)) {
			orders.setStatus(OrderConstant.CANCELLED);
			ordersMapper.updateById(orders);
		}
	}

	public void confirmOrde(String number) {
		// 确认收货
		QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("number", number);
		Orders orders = ordersMapper.selectOne(queryWrapper);
		if (Objects.equals(orders.getStatus(), OrderConstant.DELIVERY)) {
			orders.setStatus(OrderConstant.COMPLETED);
			ordersMapper.updateById(orders);
		}
	}
}




